package com.lvt.example.thinking.in.spring.questions.L24.resttemplate;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

/**
 * desc：class
 *
 * @author LT
 * @date 2022/10/25
 */
public class Test2 {
    public static void main(String[] args) {

        //解析 URL 的关键点在于 UriComponentsBuilder#fromUriString 实现
//        String url = "http://localhost:8080/hi2?para1=1#2";
//        HttpEntity<?> entity = new HttpEntity<>(null);
//
//        RestTemplate restTemplate = new RestTemplate();
//        HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
//
//        System.out.println(response.getBody());

        // 修复 可以参考 UriComponentsBuilder#fromHttpUrl，并与之前使用的 UriComponentsBuilder#fromUriString 进行比较：

        String url = "http://localhost:8080/hi2?para1=1#2";
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        URI uri = builder.build().encode().toUri();
        HttpEntity<?> entity = new HttpEntity<>(null);

        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET,entity,String.class);

        System.out.println(response.getBody());
    }
}
